#pragma once

#include <iostream>
#include <unistd.h>


using namespace std;

typedef void(*work_t)();  //函数指针类型
typedef void(*task_t)();  //函数指针类型

void PrintLog()
{
    cout<<"Printf log task"<<endl;
}

void reloadConf()
{
    cout<<"reload Conf task"<<endl;
}

void ConnectMySQL()
{
    cout<< "Connect MySQL task"<<endl;
}


task_t Task[3] = {PrintLog,reloadConf,ConnectMySQL};


int _NextTask()
{
    //三个任务随机一个
    return rand()%3;
}


void worker()
{
    while(true)
    {
        int command_code = 0;
        //执行函数之前，已经进行dup2
        ssize_t n = read(0,&command_code,sizeof(command_code));//返回值是读取字节数
        if(n==sizeof(command_code))
        {
            //说明成功读取
            if(command_code>=3)
                continue;//超出任务数，重新读取
            Task[command_code]();//执行任务
        }
        else if(n==0)
        {
            cout<<"sub Process:"<<getpid()<<" Qiut Success"<<endl;
            break;  
        }

        
    }
}
